package com.blb.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.blb.entity.Company;
import com.blb.entity.Staff;
import com.blb.entity.User;
import com.blb.service.ICompanyService;
import com.blb.service.IStaffService;
import com.blb.service.IUserService;
import com.blb.utils.Response;
import com.blb.utils.ResponseEntity;
import com.blb.utils.ShiroUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController {

    @Autowired
    private ICompanyService companyService;
    @Autowired
    private IStaffService staffService;

    @ResponseBody
    @RequestMapping("/login1")
    public String login(String account, String password, String code, HttpServletRequest req, RedirectAttributes model) {
        HttpSession session = req.getSession();
        //获取图片验证码
        String code1 = (String) session.getAttribute("code");
        //移除验证码的唯一性
        session.removeAttribute("code");
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(account, password);
        if (!(code.toLowerCase()).equals(code1)) {
            return "{\"result\":false}";
        } else {
            return "{\"result\":true}";
        }
    }

    @ResponseBody
    @RequestMapping("/login2")
    public String login2(String account, String password) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(account, password);
        subject.login(token);
        return "{\"result\":true}";
    }

    @ResponseBody
    @RequestMapping("/login3")
    public String login3() {
        User user = ShiroUtils.getUser();
        Company company = companyService.getById(ShiroUtils.getCompany());
        if (user.getStaffId() != null && !"".equals(user.getStaffId().trim())){
            Staff staff = staffService.getById(user.getStaffId());
            if ("0".equals(staff.getNumberId())){
                return "{\"result\":\"no\"}";
            }
        }
        if (!"已启用".equals(company.getCompanyStatus())){
            return "{\"result\":false}";
        }else {
            return "{\"result\":true}";
        }
    }

}
